<?php
function get_graph($id, $my_team_id)
{

ob_start();
$fontfile = "1";
$image_width = 680;
$image_height = 200;
$graph_begin = 20;
$graph_width = $image_width - $graph_begin;

$query = "SELECT * FROM Championship WHERE id = '$id';";
$result_champ = mysql_query($query);

$num = mysql_numrows($result_champ);

if ($num == 0)
{
	die("Couldn't find championship $championship");
}
$point_win = mysql_result($result_champ, 0, "point_win");
$point_draw = mysql_result($result_champ, 0, "point_draw");
$point_loss = mysql_result($result_champ, 0, "point_loss");

echo "<DIV class='tab_container' ID='tab_graphs'>\n";
$query = "SELECT * FROM Phase where `championship` = '$id' ORDER BY `order`;";
$result_phase = mysql_query($query);
$num_phase = mysql_numrows($result_phase);
for ($j = 0; $j < $num_phase; $j++)
{
	$id_phase = mysql_result($result_phase, $j, "id");
	$name_phase = mysql_result($result_phase, $j, "name");

	$team_table = array();

	$query = "SELECT * FROM `Group`, team_group WHERE `phase` = '$id_phase' AND team_group.`group` = `Group`.id AND team_group.team = '$my_team_id';";
	$result_group = mysql_query($query);
	$num_group = mysql_numrows($result_group);
	for ($k = 0; $k < $num_group; $k++)
	{
		$id_group = mysql_result($result_group, $k, "id");
		$name_group = mysql_result($result_group, $k, "name");
		$promoted = mysql_result($result_group, $k, "promoted");
		$relegated = mysql_result($result_group, $k, "relegated");

#		echo "<h3>$name_group</h3>\n";

		$group_teams = array();

		$query = "SELECT Team.id, Team.name, team_group.add_sub FROM Team, team_group WHERE team_group.group = '$id_group' AND Team.id = team_group.team;";
		$result_teams = mysql_query($query);
		$num_teams = mysql_numrows($result_teams);
		for ($l = 0; $l < $num_teams; $l++)
		{
			$team_id = mysql_result($result_teams, $l, "Team.id");
			$team_name = mysql_result($result_teams, $l, "Team.name");
			$add_sub = mysql_result($result_teams, $l, "team_group.add_sub");

			$group_teams[] = $team_id;

			$team_table[$team_id]["name"] = $team_name;
			if (!isset($team_table[$team_id]["g"]))
				$team_table[$team_id]["g"] = 0;
			if (!isset($team_table[$team_id]["pt"]))
				$team_table[$team_id]["pt"] = 0;
			if (!isset($team_table[$team_id]["w"]))
				$team_table[$team_id]["w"] = 0;
			if (!isset($team_table[$team_id]["d"]))
				$team_table[$team_id]["d"] = 0;
			if (!isset($team_table[$team_id]["l"]))
				$team_table[$team_id]["l"] = 0;
			if (!isset($team_table[$team_id]["gf"]))
				$team_table[$team_id]["gf"] = 0;
			if (!isset($team_table[$team_id]["ga"]))
				$team_table[$team_id]["ga"] = 0;
			if (!isset($team_table[$team_id]["gp"]))
				$team_table[$team_id]["gp"] = 0;
			if (!isset($team_table[$team_id]["g_away"]))
				$team_table[$team_id]["g_away"] = 0;

			$team_table[$team_id]["pt"] += $add_sub;
			$team_table[$team_id]["add_sub"] = $add_sub;

			$team_table[$team_id]["gd"] = $team_table[$team_id]["gf"] - $team_table[$team_id]["ga"];
		}
	}

	$games = array();
	$query = "SELECT * FROM Game, Team Home, Team Away WHERE phase = $id_phase AND played = 'played' AND Home.id = Game.home AND Away.id = Game.away ORDER BY date;";
	$result_game = mysql_query($query);
	$num_game = mysql_numrows($result_game);
        $found = false;
	for ($k = 0; $k < $num_game; $k++)
	{
		$id_game = mysql_result($result_game, $k, "id");
		$home = mysql_result($result_game, $k, "home");
		$away = mysql_result($result_game, $k, "away");
		$home_name = mysql_result($result_game, $k, "Home.name");
		$away_name = mysql_result($result_game, $k, "Away.name");
		$phase = mysql_result($result_game, $k, "phase");
		$home_score = mysql_result($result_game, $k, "home_score");
		$away_score = mysql_result($result_game, $k, "away_score");
		$home_pen = mysql_result($result_game, $k, "home_pen");
		$away_pen = mysql_result($result_game, $k, "away_pen");

		if ($home == $my_team_id or $away == $my_team_id)
		{
                        $found = true;
			$team_table = multi_sort($team_table, array("pt", "w", "gd", "gf", "gp", "g_away", array("name", true)), false);

			$pos = 0;
			while (list($key, $value) = each($team_table))
			{
				$pos++;
				if ($key == $my_team_id) break;
			}

			if ($home == $my_team_id)
			{
				if ($home_score > $away_score) $result = "w";
				else if ($home_score < $away_score) $result = "l";
				else $result = "d";
				$home_name = "<b>$home_name</b>";
			}
			else
			{
				if ($home_score < $away_score) $result = "w";
				else if ($home_score > $away_score) $result = "l";
				else $result = "d";
				$away_name = "<b>$away_name</b>";
			}
			if (count($games)) $games[count($games)-1][1] = $pos;
			$games[] = array($result, 0, "$home_name $home_score x $away_score $away_name");
		}

		if (isset($team_table[$home]))
		{
			$team_table[$home]["gf"] += $home_score;
			$team_table[$home]["ga"] += $away_score;
			$team_table[$home]["g"] += 1;
			$team_table[$home]["gp"] += $home_pen;
			if ($home_score > $away_score)
			{
				$team_table[$home]["w"] += 1;
				$team_table[$home]["pt"] += $point_win;
			}
			else if ($home_score < $away_score)
			{
				$team_table[$home]["l"] += 1;
			}
			else
			{
				$team_table[$home]["d"] += 1;
				$team_table[$home]["pt"] += $point_draw;
			}
		}

		if (isset($team_table[$away]))
		{
			$team_table[$away]["gf"] += $away_score;
			$team_table[$away]["ga"] += $home_score;
			$team_table[$away]["g"] += 1;
			$team_table[$away]["gp"] += $away_pen;
			if ($home_score < $away_score)
			{
				$team_table[$away]["w"] += 1;
				$team_table[$away]["pt"] += $point_win;
			}
			else if ($home_score > $away_score)
			{
				$team_table[$away]["l"] += 1;
			}
			else
			{
				$team_table[$away]["d"] += 1;
				$team_table[$away]["pt"] += $point_draw;
			}
		}
	}

        if ($found) {
	$team_table = multi_sort($team_table, array("pt", "w", "gd", "gf", "gp", "g_away", array("name", true)), false);
	$pos = 0;
	while (list($key, $value) = each($team_table))
	{
		$pos++;
		if ($key == $my_team_id) break;
	}
	if (count($games)) $games[count($games)-1][1] = $pos;

	echo "<DIV ID='$name_phase' class='tab_page'>\n";

	if (count($team_table))
	{
		echo "<MAP NAME='map$j'>";

                $im = @imagecreatetruecolor($image_width, $image_height)
                  or die("Cannot Initialize new GD image stream");
                $white_color = imagecolorallocate($im, 255, 255, 255);
                $black_color = imagecolorallocate($im, 0, 0, 0);
                $yellow_color = imagecolorallocate($im, 255, 255, 0);
                imagefilledrectangle($im, 0, 0, $image_width, $image_height, $white_color);

		$oldx = null;
		$oldy = null;
		$points = 0;
		for ($k = 0; $k < count($games); $k++)
		{
			if ($games[$k][0] == "w")
			{
				$color = imagecolorallocate($im, 0, 0, 255);
				$points += $point_win;
			}
			else if ($games[$k][0] == "d")
			{
				$color = imagecolorallocate($im, 190, 190, 190);
				$points += $point_draw;
			}
			else
			{
				$color = imagecolorallocate($im, 255, 0, 0);
			}

# Points
			$x1 = $graph_width * $k / count($games) + $graph_begin;
			$y1 = $image_height - ($points / count($games) / 3 * $image_height);
			$x2 = $x1 + $graph_width / count($games);
			$y2 = $image_height;
			imagefilledrectangle($im, $x1, $y1, $x2, $y2, $color);
			imagerectangle($im, $x1, $y1, $x2-1, $y2-1, $black_color);

			echo "<AREA TITLE='header=[<center>".ordinal($games[$k][1])." - $points pts</center>] body=[".$games[$k][2]."]' SHAPE=RECT COORDS='$x1,0,$x2,$y2'>\n";

# Position
			$x1 = $graph_width * ($k + 0.5) / count($games) + $graph_begin;
			$y1 = $image_height - ((count($team_table) - $games[$k][1] + 0.5) / count($team_table) * $image_height);
			$w = $graph_width / count($games);
			if ($w > 15) $w = 15;
			$w = $w - 3;

			imagefilledellipse($im, $x1, $y1, $w, $w, $yellow_color);

			imagefilledellipse($im, $x1, $y1, 2, 2, $black_color);

			if ($oldx and $oldy)
			{
				imageline($im, $oldx, $oldy, $x1, $y1, $black_color);
			}

			$oldx = $x1;
			$oldy = $y1;
		}

		$fontsize = 1 / count($team_table) * $image_height;
		$fontsize -= 2;
		if ($fontsize > 15) $fontsize = 15;
		for ($k = 0; $k < count($team_table); $k++)
		{
			$x1 = $graph_begin;
			$y1 = $image_height - (($k + 0.5) / count($team_table) * $image_height);
			$x2 = $graph_begin + 2;
			$y2 = $y1;
			imageline($im, $x1, $y1, $x2, $y2, $black_color);

			$y1 = $image_height - (($k + 1) / count($team_table) * $image_height);
			imagestring($im, $fontfile, 0, $y1, count($team_table) - $k, $black_color);
		}
		echo "</MAP>\n";

		$transp_green = imagecolorallocatealpha($im, 0, 255, 0, 90);
		$transp_red = imagecolorallocatealpha($im, 180, 0, 0, 90);
		imagefilledrectangle($im, $graph_begin, 0, $image_width, ($promoted) / count($team_table) * $image_height, $transp_green);
		imagefilledrectangle($im, $graph_begin, $image_height - (($relegated) / count($team_table) * $image_height), $image_width, $image_height, $transp_red);
	}

        imagerectangle($im, $graph_begin, 0, $image_width-1, $image_height-1, $black_color);

        ob_start();
        imagepng($im);
        $image_blob = ob_get_contents();
        ob_end_clean();
        imagedestroy($im);

        echo "<IMG SRC='data:image/png;base64,".base64_encode($image_blob)."' USEMAP='map$j'>\n";
        echo "</DIV>";
        }
}

echo "</DIV>\n";

$ret = ob_get_contents();
ob_end_clean();

return $ret;
}

function ordinal($n)
{
	if ($n > 10 and $n < 20) return "$n"."th";
	if ($n % 10 == 1) return "$n"."st";
	if ($n % 10 == 2) return "$n"."nd";
	if ($n % 10 == 3) return "$n"."rd";
	return "$n"."th";
}
?> 
